﻿using ByrForumAPI.Entity;
using ByrForumAPI.MetaData;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ByrForumAPI
{
    public class ForumClient
    {
        private const string BASIC_URL = "http://api.byr.cn";

        private string appKey;

        private string userId;

        private string password;

        public ForumClient()
        {
            this.appKey = "78e223c052793f0b";
            this.userId = "guest";
            this.password = string.Empty;
        }


        private string Request(string url)
        { 
            // build request url
            string rawUrl = string.Format("{0}{1}?appkey={2}", BASIC_URL, url, this.appKey);
            UriBuilder uri = new UriBuilder(rawUrl);
            string result = string.Empty;
            HttpWebRequest http = WebRequest.Create(uri.Uri) as HttpWebRequest;
            http.ServicePoint.Expect100Continue = false;
            http.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)";
            http.Credentials = new NetworkCredential(this.userId, this.password);
            http.Method = "GET";
            using (WebResponse response = http.GetResponse())
            {

                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    try
                    {
                        result = reader.ReadToEnd();
                    }
                    finally
                    {
                        reader.Close();
                    }
                }
            }
            return result;
        }

        // 获取所有根分区的信息
        public List<Section> GetSectionInfo()
        {
            string sectionRequestUrl = @"/section.json";
            string response = Request(sectionRequestUrl);
            JObject o = JObject.Parse(response);
            return JsonConvert.DeserializeObject<List<Section>>(o["section"].ToString());
        
        }

        public SectionEntity GetSectionInfo(string sectionName)
        {
            string sectionRequestUrl = string.Format("/section/{0}.json", sectionName);
            string response = Request(sectionRequestUrl);
            return JsonConvert.DeserializeObject<SectionEntity > (response);
        }

        public BoardEntity GetBoardInfo(string boardName)
        {
            string boardRequestUrl = string.Format("/board/{0}.json", boardName);
            string response = Request(boardRequestUrl);
            return JsonConvert.DeserializeObject<BoardEntity>(response);
        }

        // 获取指定文章的信息
        public Article GetBoardArticle(string boardName, int articleId)
        {
            string requestUrl = string.Format("/article/{0}/{1}.json", boardName, articleId);
            string response = Request(requestUrl);
            return JsonConvert.DeserializeObject<Article>(response);
        }


        // 获取指定主题的信息
        public ThreadArticleEntity GetThreadArticles(string boardName, int id)
        {
            string requestUrl = string.Format("/threads/{0}/{1}.json", boardName, id);
            string response = Request(requestUrl);
            return JsonConvert.DeserializeObject<ThreadArticleEntity>(response);
        }
    }
}
